<?php 
	Class Package extends CI_Controller{
		public function __construct(){
			parent::__construct();
            $this->load->library("pager");
            $this->load->helper('main_helper');
			$this->load->model("model_hotel");
			$this->load->model("model_hotelroom");
			$this->load->model("model_hotelroomprice");
			$this->load->model("model_restaurant");
			$this->load->model("model_restaurantreview");
			$this->load->model("model_activity");
			$this->load->model("model_activityreview");
			$this->load->model("model_activityprice");
            $this->load->model('model_package');
            $this->load->model('model_flight');
            $this->load->model('model_car');
            $this->load->model('model_guild');
            $this->load->model('model_packagereview');
			$this->load->model('model_packagebanner');
			$this->load->model('model_packageprice');
		}
		public function updateitinerary(){
			var_dump($_POST);
			//$this->load->view();
		}
		public function updatecart(){
			$_SESSION["cart"]=isset($_POST["data"])?$_POST["data"]:array();
		}
		public function customize(){
			$date=isset($_GET["d"])?$_GET["d"]:date("Y-d-m");
			$numberadult=isset($_GET["adult"])?$_GET["adult"]:0;
			$numberchild=isset($_GET["child"])?$_GET["child"]:0;
			$numberinfant=isset($_GET["infant"])?$_GET["infant"]:0;

			$listdata=array();
			$listdata["adult"]=$numberadult;
			$listdata["child"]=$numberchild;
			$listdata["infant"]=$numberinfant;
			$listdata["yangon"]["hotel"]=$this->model_hotel->listtopbyprovide_orderby(100000,0,"yangon","name");
			$listdata["bagan"]["hotel"]=$this->model_hotel->listtopbyprovide_orderby(100000,0,"bagan","name");
			$listdata["madalay"]["hotel"]=$this->model_hotel->listtopbyprovide_orderby(100000,0,"madalay","name");
			$listdata["inle"]["hotel"]=$this->model_hotel->listtopbyprovide_orderby(100000,0,"inle","name");
			
			$listdata["yangon"]["activity"]=$this->model_activity->listtopbyprovide_orderby(100000,0,"yangon","name");
			$listdata["bagan"]["activity"]=$this->model_activity->listtopbyprovide_orderby(100000,0,"bagan","name");
			$listdata["madalay"]["activity"]=$this->model_activity->listtopbyprovide_orderby(100000,0,"madalay","name");
			$listdata["inle"]["activity"]=$this->model_activity->listtopbyprovide_orderby(100000,0,"inle","name");
			
			$listdata["yangon"]["restaurant"]=$this->model_restaurant->listtopbyprovide_orderby(100000,0,"yangon","name");
			$listdata["bagan"]["restaurant"]=$this->model_restaurant->listtopbyprovide_orderby(100000,0,"bagan","name");
			$listdata["madalay"]["restaurant"]=$this->model_restaurant->listtopbyprovide_orderby(100000,0,"madalay","name");
			$listdata["inle"]["restaurant"]=$this->model_restaurant->listtopbyprovide_orderby(100000,0,"inle","name");
			
			//Get price for hotel
			foreach($listdata["yangon"]["hotel"] as &$hotel){
				$p=$this->model_hotelroomprice->getminpricebyhotelidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["price"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["bagan"]["hotel"] as &$hotel){
				$p=$this->model_hotelroomprice->getminpricebyhotelidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["price"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["madalay"]["hotel"] as &$hotel){
				$p=$this->model_hotelroomprice->getminpricebyhotelidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["price"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["inle"]["hotel"] as &$hotel){
				$p=$this->model_hotelroomprice->getminpricebyhotelidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["price"];
				}else $hotel["price"]="N/A";
			}
			//Get star for restaurant
			foreach($listdata["yangon"]["restaurant"] as &$hotel){
				$p=$this->model_restaurantreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["bagan"]["restaurant"] as &$hotel){
				$p=$this->model_restaurantreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["madalay"]["restaurant"] as &$hotel){
				$p=$this->model_restaurantreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["inle"]["restaurant"] as &$hotel){
				$p=$this->model_restaurantreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			//Get star for activity
			foreach($listdata["yangon"]["activity"] as &$hotel){
				$p=$this->model_activityreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["bagan"]["activity"] as &$hotel){
				$p=$this->model_activityreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["madalay"]["activity"] as &$hotel){
				$p=$this->model_activityreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			foreach($listdata["inle"]["activity"] as &$hotel){
				$p=$this->model_activityreview->getMarkbyrestaurantid($hotel["id"]);
				if(count($p)>0){
					$hotel["star"]=ceil($p["star"]);
				}else $hotel["star"]="N/A";
			}
			//Get price for activity
			foreach($listdata["yangon"]["activity"] as &$hotel){
				$p=$this->model_activityprice->getminpricebyactivityidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["pricefor1"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["bagan"]["activity"] as &$hotel){
				$p=$this->model_activityprice->getminpricebyactivityidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["pricefor1"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["madalay"]["activity"] as &$hotel){
				$p=$this->model_activityprice->getminpricebyactivityidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["pricefor1"];
				}else $hotel["price"]="N/A";
			}
			foreach($listdata["inle"]["activity"] as &$hotel){
				$p=$this->model_activityprice->getminpricebyactivityidanddate($hotel["id"],$date);
				if(count($p)>0){
					$hotel["price"]=$p["pricefor1"];
				}else $hotel["price"]="N/A";
			}
			
			
					
			$this->load->view("package/customize",$listdata);
		}
		public function package_customize(){
				$data["count_total_accomodation_yangon"]=$this->model_hotel->countallhotelbyprovide('yangon');
				$data["count_total_restaurantspa_yangon"]=$this->model_restaurant->countallbyprovide('yangon');
				$data["count_total_activities_yangon"]=$this->model_activity->countallbyprovide('yangon');
				
				$data["count_total_accomodation_bagan"]=$this->model_hotel->countallhotelbyprovide('bagan');
				$data["count_total_restaurantspa_bagan"]=$this->model_restaurant->countallbyprovide('bagan');
				$data["count_total_activities_bagan"]=$this->model_activity->countallbyprovide('bagan');
				
				$data["count_total_accomodation_madalay"]=$this->model_hotel->countallhotelbyprovide('madalay');
				$data["count_total_restaurantspa_madalay"]=$this->model_restaurant->countallbyprovide('madalay');
				$data["count_total_activities_madalay"]=$this->model_activity->countallbyprovide('madalay');
				
				$data["count_total_accomodation_inle"]=$this->model_hotel->countallhotelbyprovide('inle');
				$data["count_total_restaurantspa_inle"]=$this->model_restaurant->countallbyprovide('inle');
				$data["count_total_activities_inle"]=$this->model_activity->countallbyprovide('inle');
				
				if(isset($_GET["orderby_yangon_accomodations"])){
					$data["listtop_accomodation_yangon"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'yangon',$_GET["orderby_yangon_accomodations"]);							
				}else{ $data["listtop_accomodation_yangon"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'yangon','id');}
								
				if(count($data["listtop_accomodation_yangon"])>0){
					foreach($data["listtop_accomodation_yangon"] as &$list_accomodation_yangon){
						$list_accomodation_yangon["price"]=$this->model_hotelroomprice->getpricebyroomid($list_accomodation_yangon["id"]);							
					}
				}
				
				if(isset($_GET["orderby_yangon_restaurantspa"])){
					$data["listtop_restaurantspa_yangon"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'yangon',$_GET["orderby_yangon_restaurantspa"]);							
				}else{ $data["listtop_restaurantspa_yangon"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'yangon','id');}
				
				if(count($data["listtop_restaurantspa_yangon"])>0){
					foreach($data["listtop_restaurantspa_yangon"] as &$list_restaurantspa_yangon){
						$liststar=$this->model_restaurantreview->markbyid($list_restaurantspa_yangon["id"]);
						if(count($liststar)>0){
						$totalmark_yangon_restaurantspa=0;		
						foreach($liststar as $total){
							$totalmark_yangon_restaurantspa+=$total["mark"];
						}
						$list_restaurantspa_yangon["star"]=floor($totalmark_yangon_restaurantspa/count($liststar));}
						else $list_restaurantspa_yangon["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_yangon_activities"])){
					$data["listtop_activities_yangon"]=$this->model_activity->listtopbyprovide_orderby(5,0,'yangon',$_GET["orderby_yangon_activities"]);					
				}else{$data["listtop_activities_yangon"]=$this->model_activity->listtopbyprovide_orderby(5,0,'yangon','id');}
				
				if(count($data["listtop_activities_yangon"])>0){
					foreach($data["listtop_activities_yangon"] as &$list_activities_yangon){
						$liststar=$this->model_activityreview->markbyid($list_activities_yangon["id"]);
						if(count($liststar)>0){
						$totalmark_yangon_activities=0;		
						foreach($liststar as $total){
							$totalmark_yangon_activities+=$total["mark"];
						}
						$list_activities_yangon["star"]=floor($totalmark_yangon_activities/count($liststar));}
						else $list_activities_yangon["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_bagan_accomodations"])){
					$data["listtop_accomodation_bagan"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'bagan',$_GET["orderby_yangon_accomodations"]);							
				}else{ $data["listtop_accomodation_bagan"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'bagan','id');}
								
				if(count($data["listtop_accomodation_bagan"])>0){
					foreach($data["listtop_accomodation_bagan"] as &$list_accomodation_bagan){
						$list_accomodation_bagan["price"]=$this->model_hotelroomprice->getpricebyroomid($list_accomodation_bagan["id"]);							
					}
				}
				
				if(isset($_GET["orderby_bagan_restaurantspa"])){
					$data["listtop_restaurantspa_bagan"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'bagan',$_GET["orderby_bagan_restaurantspa"]);							
				}else{ $data["listtop_restaurantspa_bagan"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'bagan','id');}
				
				if(count($data["listtop_restaurantspa_bagan"])>0){
					foreach($data["listtop_restaurantspa_bagan"] as &$list_restaurantspa_bagan){
						$liststar=$this->model_restaurantreview->markbyid($list_restaurantspa_bagan["id"]);
						if(count($liststar)>0){
						$totalmark_bagan_restaurantspa=0;		
						foreach($liststar as $total){
							$totalmark_bagan_restaurantspa+=$total["mark"];
						}
						$list_restaurantspa_bagan["star"]=floor($totalmark_bagan_restaurantspa/count($liststar));}
						else $list_restaurantspa_bagan["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_bagan_activities"])){
					$data["listtop_activities_bagan"]=$this->model_activity->listtopbyprovide_orderby(5,0,'bagan',$_GET["orderby_bagan_activities"]);
				}else{$data["listtop_activities_bagan"]=$this->model_activity->listtopbyprovide_orderby(5,0,'bagan','id');}
				
				if(count($data["listtop_activities_bagan"])>0){
					foreach($data["listtop_activities_bagan"] as &$list_activities_bagan){
						$liststar=$this->model_restaurantreview->markbyid($list_activities_bagan["id"]);
						if(count($liststar)>0){
						$totalmark_bagan_activities=0;		
						foreach($liststar as $total){
							$totalmark_bagan_activities+=$total["mark"];
						}
						$list_activities_bagan["star"]=floor($totalmark_bagan_activities/count($liststar));}
						else $list_activities_bagan["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_madalay_accomodations"])){
					$data["listtop_accomodation_madalay"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'madalay',$_GET["orderby_yangon_accomodations"]);							
				}else{ $data["listtop_accomodation_madalay"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'madalay','id');}
								
				if(count($data["listtop_accomodation_madalay"])>0){
					foreach($data["listtop_accomodation_madalay"] as &$list_accomodation_madalay){
						$list_accomodation_madalay["price"]=$this->model_hotelroomprice->getpricebyroomid($list_accomodation_madalay["id"]);							
					}
				}
				
				if(isset($_GET["orderby_madalay_restaurantspa"])){
					$data["listtop_restaurantspa_madalay"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'madalay',$_GET["orderby_madalay_restaurantspa"]);							
				}else{ $data["listtop_restaurantspa_madalay"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'madalay','id');}
				
				if(count($data["listtop_restaurantspa_madalay"])>0){
					foreach($data["listtop_restaurantspa_madalay"] as &$list_restaurantspa_madalay){
						$liststar=$this->model_restaurantreview->markbyid($list_restaurantspa_madalay["id"]);
						if(count($liststar)>0){
						$totalmark_madalay_restaurantspa=0;		
						foreach($liststar as $total){
							$totalmark_madalay_restaurantspa+=$total["mark"];
						}
						$list_restaurantspa_madalay["star"]=floor($totalmark_madalay_restaurantspa/count($liststar));}
						else $list_restaurantspa_madalay["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_madalay_activities"])){
					$data["listtop_activities_madalay"]=$this->model_activity->listtopbyprovide_orderby(5,0,'madalay',$_GET["orderby_madalay_activities"]);
				}else{$data["listtop_activities_madalay"]=$this->model_activity->listtopbyprovide_orderby(5,0,'madalay','id');}
				
				if(count($data["listtop_activities_madalay"])>0){
					foreach($data["listtop_activities_madalay"] as &$list_activities_madalay){
						$liststar=$this->model_restaurantreview->markbyid($list_activities_madalay["id"]);
						if(count($liststar)>0){
						$totalmark_madalay_activities=0;		
						foreach($liststar as $total){
							$totalmark_madalay_activities+=$total["mark"];
						}
						$list_activities_madalay["star"]=floor($totalmark_madalay_activities/count($liststar));}
						else $list_activities_madalay["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_inle_accomodations"])){
					$data["listtop_accomodation_inle"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'inle',$_GET["orderby_yangon_accomodations"]);							
				}else{ $data["listtop_accomodation_inle"]=$this->model_hotel->listtopbyprovide_orderby(5,0,'inle','id');}
								
				if(count($data["listtop_accomodation_inle"])>0){
					foreach($data["listtop_accomodation_inle"] as &$list_accomodation_inle){
						$list_accomodation_inle["price"]=$this->model_hotelroomprice->getpricebyroomid($list_accomodation_inle["id"]);							
					}
				}
				
				if(isset($_GET["orderby_inle_restaurantspa"])){
					$data["listtop_restaurantspa_inle"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'inle',$_GET["orderby_inle_restaurantspa"]);							
				}else{ $data["listtop_restaurantspa_inle"]=$this->model_restaurant->listtopbyprovide_orderby(5,0,'inle','id');}
				
				if(count($data["listtop_restaurantspa_inle"])>0){
					foreach($data["listtop_restaurantspa_inle"] as &$list_restaurantspa_inle){
						$liststar=$this->model_restaurantreview->markbyid($list_restaurantspa_inle["id"]);
						if(count($liststar)>0){
						$totalmark_inle_restaurantspa=0;		
						foreach($liststar as $total){
							$totalmark_inle_restaurantspa+=$total["mark"];
						}
						$list_restaurantspa_inle["star"]=floor($totalmark_inle_restaurantspa/count($liststar));}
						else $list_restaurantspa_inle["star"]=0;
					}
				}
				
				if(isset($_GET["orderby_inle_activities"])){
					$data["listtop_activities_inle"]=$this->model_activity->listtopbyprovide_orderby(5,0,'inle',$_GET["orderby_inle_activities"]);
				}else{$data["listtop_activities_inle"]=$this->model_activity->listtopbyprovide_orderby(5,0,'inle','id');}
				
				if(count($data["listtop_activities_inle"])>0){
					foreach($data["listtop_activities_inle"] as &$list_activities_inle){
						$liststar=$this->model_restaurantreview->markbyid($list_activities_inle["id"]);
						if(count($liststar)>0){
						$totalmark_inle_activities=0;		
						foreach($liststar as $total){
							$totalmark_inle_activities+=$total["mark"];
						}
						$list_activities_inle["star"]=floor($totalmark_inle_activities/count($liststar));}
						else $list_activities_inle["star"]=0;
					}
				}

			$data["template"]="package_customize_form";
			$this->load->view("package/package_customize_form",$data);
		}
		public function package_search_result(){
			if(isset($_GET['provide']) && isset($_GET['cate'])){
				$cate=intval($_GET['cate']);	
				$data['data']=$this->model_package->datasearch($_GET['provide'],$cate,'length',10,0);
				$data['total']=$this->model_package->datasearch($_GET['provide'],$cate,'length',1000000000,0);				
			}
			$data['listbanner']=array();
			foreach($data['data'] as $item){
				$data['listbanner']=$this->model_packagebanner->gettopbanner_search($item['id']);	
			}
			$data['listname_restaurant']=$this->model_restaurant->listall_name();            

			$data["template"]="package/package_search_result";
			$this->load->view("layout",$data);
		}
		public function detail($idpackage){	
			$data['idpackage']=intval($idpackage);
			$today=getdate();
			if(isset($_GET['from'])) $from=date('Y-m-d',strtotime($_GET['from']));
			else $from= $today['year']."-".$today['mon']."-".$today['mday'];
			if(isset($idpackage)){
				$data['data']=$this->model_package->getminprice_detail(intval($idpackage),$from);
				$data['listprice']=$this->model_packageprice->getallbypackageid_date($data['data']['id'],$from);
				$data['banner']=$this->model_packagebanner->getallbypackageid($data['data']['id']);
			}
			$data["restaurant"]=$this->model_restaurant->gettopByprovide_status($data['data']['destination'],0,6,0);
			$data["activities"]=$this->model_activity->gettopByprovide_status($data['data']['destination'],0,6,0);
			$data["package"]=$this->model_package->gettopByprovide_status($data['data']['destination'],0,6,0);
			$data["flight"]=$this->model_flight->gettopByprovide_status($data['data']['destination'],0,6,0);
			$data["car"]=$this->model_car->gettopByprovide_status($data['data']['destination'],0,3,0);
			$data["guide"]=$this->model_guild->gettopByprovide_status($data['data']['destination'],0,3,0);
			$data['listname_restaurant']=$this->model_restaurant->listall_name();
			
			$data["template"]="package/package_detail";
			$this->load->view("layout",$data);
		}
		
		
		public function package_customize_form_create_interary(){
			//var_dump($_SESSION["cart"]);die();
			$data=array();
			$data["cart"]=isset($_SESSION["cart"])?$_SESSION["cart"]:array();
			$data["hotel"]=array();
			$data["restaurant"]=array();
			$data["activity"]=array();
			$data["guide"]=array();
			$data["place"]=array(
				array("id"=>"yangon","name"=>"YANGON"),
				array("id"=>"bagan","name"=>"BAGAN"),
				array("id"=>"madalay","name"=>"MADALAY"),
				array("id"=>"inle","name"=>"INLE"),
			);
			foreach($_SESSION["cart"] as $d){
				if(isset($d["listhotel"])){
					foreach($d["listhotel"] as $h){
						$data["hotel"][]=array("id"=>$h["id"],"name"=>$h["name"]);
					}
				}
				if(isset($d["listrestaurant"])){
					foreach($d["listrestaurant"] as $h){
						$data["restaurant"][]=array("id"=>$h["id"],"name"=>$h["name"]);
					}
				}
				if(isset($d["listactivity"])){
					foreach($d["listactivity"] as $h){
						$data["activity"][]=array("id"=>$h["id"],"name"=>$h["name"]);
					}
				}
			}
			$listguide=$this->model_guild->listall(100000,0);
			foreach($listguide as $g){
				$data["guide"][]=array("id"=>$g["id"],"name"=>$g["name"]);
			}
			$this->load->view("package/package_customize_form_create_interary",$data);
		}
		
		public function packageinmyanmar(){
			$data['speacial']=$this->model_package->countbycate('1');
			$data['cultural']=$this->model_package->countbycate('2');	
			$data['adventure']=$this->model_package->countbycate('3');	
			$data['filgrimage']=$this->model_package->countbycate('4');	
			$data['eco']=$this->model_package->countbycate('5');	
			$data['listname_restaurant']=$this->model_restaurant->listall_name();
			$data["template"]="package/myanmar";
			$this->load->view("layout",$data);
		}
	}
?>